package cn.com.blueInfo.bpm.system.convert;

import cn.com.blueInfo.bpm.common.convert.BaseConvert;
import cn.com.blueInfo.bpm.system.entity.DynamicFormField;
import cn.com.blueInfo.bpm.system.request.DynamicFormFieldDTO;
import cn.com.blueInfo.bpm.system.response.DynamicFormFieldVO;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

/**
 * 动态表单字段表 对象转换
 * @author suxch
 * @since 2025-08-31 16:20:56
 */
@Mapper(componentModel = "spring")
public interface DynamicFormFieldConvert
        extends BaseConvert<DynamicFormField, DynamicFormFieldDTO, DynamicFormFieldVO> {

    @Mapping(source = "fieldName", target = "fieldName", qualifiedByName = "clientToDataBase")
    DynamicFormField dtoToEntity(DynamicFormFieldDTO dto);

//    @Mapping(source = "fieldName", target = "fieldName", qualifiedByName = "clientToDataBase")
    DynamicFormField dtoToEntity(DynamicFormFieldDTO.UpdatePartialDTO dto);

    @Mapping(source = "status", target = "status", qualifiedByName = "enumToDesc")
    @Mapping(source = "deleted", target = "deleted", qualifiedByName = "enumToDesc")
    @Mapping(source = "required", target = "required", qualifiedByName = "enumToDesc")
    @Mapping(source = "hidden", target = "hidden", qualifiedByName = "enumToDesc")
    @Mapping(source = "searchable", target = "searchable", qualifiedByName = "enumToDesc")
    @Mapping(source = "listShow", target = "listShow", qualifiedByName = "enumToDesc")
    @Mapping(source = "fieldName", target = "fieldName", qualifiedByName = "dataBaseToClient")
    DynamicFormFieldVO entityToVO(DynamicFormField entity);

    @Named("dataBaseToClient")
    default String dataBaseToClient(String str) {
        if (StringUtils.isBlank(str)) return str;
        String[] strArr = str.split("_");
        StringBuilder result = new StringBuilder(strArr[0]);
        for (int i = 1; i < strArr.length; i++)
            if (!strArr[i].isEmpty()) result.append(StringUtils.capitalize(strArr[i]));
        return result.toString();
    }

    @Named("clientToDataBase")
    default String clientToDataBase(String str) {
        if (StringUtils.isBlank(str)) return str;
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) result.append('_').append(Character.toLowerCase(c));
            else result.append(c);
        }
        return result.toString();
    }

}